﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace Maticsoft.DBUtility
{
    public class Class1
    {
        //连接地址
        public static string cn = command.Properties.Resources.String1;//当前连接字串
        public static string cnw = command.Properties.Resources.String2;//外
        public static string cnl = command.Properties.Resources.String1;//内
        public static string cnwg = command.Properties.Resources.String3;//外挂地址
        public static string cnwg2 = command.Properties.Resources.String4;//外挂地址2
        //用户英文名,用户中文名
        public static string ename; //英文名
        public static string cname;  //中文名
        public static int version=41;//版本号
        public static string kstate;//描述状态,暂时还未使用
        public static string company="";//公司名称    
        public static int proint = 60;//程序加载的天数
        public static int codelength=0;//编码长度
        //数组,专门用来复制
        public static string[] ssak = new string[10];
        //专门用来执行查询
        public static void cmdexec(string strsql)
        {
            SqlConnection cna = new SqlConnection(cn);
            SqlCommand cmd = new SqlCommand(strsql, cna);
            cna.Open();
            cmd.ExecuteNonQuery();
            cna.Close();
        }
        //专门用来返回影响的行数
        public static int cmdintexec(string strsql)
        {
            SqlConnection cna = new SqlConnection(cn);
            SqlCommand cmd = new SqlCommand(strsql, cna);
            cna.Open();
            int i =cmd.ExecuteNonQuery();
            cna.Close();
            return i;
        }
        //专门用来执行查询,有返回值string
        public static string cmdscalar(string strsql)
        {
            SqlConnection cna = new SqlConnection(cn);
            SqlCommand cmd = new SqlCommand(strsql, cna);
            cna.Open();
            string stra = (string)cmd.ExecuteScalar();
            cna.Close();
            return stra;
        }
        //专门用来执行查询,有返回值int
        public static int cmdscalarint(string strsql)
        {
            SqlConnection cna = new SqlConnection(cn);
            SqlCommand cmd = new SqlCommand(strsql, cna);
            cna.Open();
            int stra = (int)cmd.ExecuteScalar();
            cna.Close();
            return stra;
        }
        //专门用来执行查询,有返回值decime
        public static decimal cmdscalarDecimal(string strsql)
        {
            SqlConnection cna = new SqlConnection(cn);
            SqlCommand cmd = new SqlCommand(strsql, cna);
            cna.Open();
            decimal dera = (decimal)cmd.ExecuteScalar();
            cna.Close();
            return dera;
        }
        //专门用来执行查询,有返回值bool
        public static bool cmdscalarbool(string strsql)
        {
            SqlConnection cna = new SqlConnection(cn);
            SqlCommand cmd = new SqlCommand(strsql, cna);
            cna.Open();
            bool dera = (bool)cmd.ExecuteScalar();
            cna.Close();
            return dera;
        }
        //专门用来执行sqldataread查询, 返回值也是sqldataread
        public static SqlDataReader sqlreader(string strread)
        {
            SqlConnection cna = new SqlConnection(cn);
            SqlCommand cmd = new SqlCommand(strread, cna);
            cna.Open();
            SqlDataReader thisread = cmd.ExecuteReader();
            cna.Close();
            return thisread;
        }
        //清除各控件内容,
        public static void qingkong(Control frm) //将所有的控件清空
        {
            foreach (Control ct in frm.Controls)
            {
                if (ct is TextBox)
                {
                    ct.Text = null;
                }
                else if (ct is DateTimePicker)
                    ct.Text = null;
                else if (ct is ComboBox)//文本框
                    ct.Text = null;
                else if (ct is CheckBox)//选择框
                    (ct as CheckBox).Checked = false;   
            }
        }
        public static void cleardatagridview(DataGridView dgv)//将datagridview清空内容
        {
            int columnCount = dgv.ColumnCount;
            int rowCount = dgv.Rows.Count;
            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < columnCount; j++)
                {
                    dgv.Rows[i].Cells[j].Value = "";
                }
            }
        }
        public static void cleardatagridviewNULL(DataGridView dgv)//将datagridview清空内容
        {
            int columnCount = dgv.ColumnCount;
            int rowCount = dgv.Rows.Count;
            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < columnCount; j++)
                {
                    dgv.Rows[i].Cells[j].Value = null;
                }
            }
        }
        public static void cleardatabindings(Control frm) //清除绑定
        {
            foreach (Control ct in frm.Controls)
            {
                ct.DataBindings.Clear();
            }
        }
        public static object sqnull(object obnull) //返回NULL 或者是DBNULL 针对要返回DBNULL,针对大部分不存在的日期
        {

            if (obnull==null)
            {
                obnull = DBNull.Value;
            }
            return obnull;
        }
        public static object sqnulldate(object obnull) //返回NULL 或者是DBNULL 针对row日期要返回DBNULL(有条件下的使用日期)
        {

            if (string.IsNullOrEmpty(obnull.ToString()))
            {
                obnull = DBNull.Value;
            }
            return obnull;
        }
        public static object sq000(string obnull) //返回数字
        {

            if (string.IsNullOrEmpty(obnull))
            {
                obnull = "0";
            }
            return obnull;
        }
        public static Decimal dec (string stext) //返回decimal数字
        {
            decimal num;
            if (string.IsNullOrEmpty(stext))
            {
                num = 0;
            }
            else
            {
                num = Convert.ToDecimal(stext);
            }
            return num;
        }
        public static bool deleform(Form his) //返回decimal数字
        {
            bool bl = false;
            DialogResult dgr = MessageBox.Show(his, "你确定要删除吗？", "删除通知", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dgr == DialogResult.Yes)
            {
                bl = true;
            }
            return bl;
        }
        public static string maxnostr(string tastr, string costr, string strformat)//tastr代表表名,constr代表字段名 strformat为 "00000"的形式,为输出格式
        {
            int lint = strformat.Length;
            string maxsql = "select isnull(max(" + costr + "),'0') from " + tastr;
            string max2 = cmdscalar(maxsql);
            //if (max2.Length < 1)
            //    max2 = "0";
            int maintt = Convert.ToInt32(max2) + 1;
            string rstr = maintt.ToString(strformat);
            return rstr;
        }
        //密码改成二进制,采用暗文不是明文
        public static byte[] EncryptPassword(string password)
        {
            UnicodeEncoding encoding = new UnicodeEncoding();
            byte[] hashBytes = encoding.GetBytes(password);
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            byte[] cryptPassword = sha1.ComputeHash(hashBytes);
            return cryptPassword;
        }

    }
}
